package com.zoe.onelink.ratelimit.sepc.impl;

import com.google.common.util.concurrent.RateLimiter;
import com.zoe.onelink.ratelimit.sepc.ApiRateLimiter;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-12-07
 */
@SuppressWarnings("UnstableApiUsage")
public class GuavaApiRateLimiter implements ApiRateLimiter {

    @Getter
    private Map<String, RateLimiter> rateLimiterMap = new HashMap<>();

    @Override
    public boolean accept(String resourceId) {
        RateLimiter rateLimiter = this.rateLimiterMap.get(resourceId);
        if (rateLimiter == null) {
            return false;
        }
        return rateLimiter.tryAcquire();
    }

}
